package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import client.CcrmApp;
import model.CustomerHistoryModel;
import model.Employee;
import model.Envelope;
import view.CustomerHistoryGui;
/**
 * customer History
 * @author liran 
 *
 */
public class CustomerHistoryController extends AbstractTransfer {
	/**
	 * go back 
	 */
	private SalesManControllers backCon = null;
	/**
	 * gui for this controller
	 */
	private CustomerHistoryGui CustHistGui = null;
	private CustomerHistoryController tempcon = null;
	private Employee EmpM = null;
	/**
	 * constructor
	 * @param back->SalesManControllers
	 * @param Custgui->CustomerHistoryGui
	 * @param EmpM1->Employee
	 */
	public CustomerHistoryController(SalesManControllers back,CustomerHistoryGui Custgui, Employee EmpM1   )
	{
		super(Custgui,EmpM1);
		EmpM = EmpM1;
		tempcon = this;
		CustHistGui = Custgui ;
		backCon = back;
		CustHistGui.addSearchAL(new SearchAction());
		CustHistGui.addBackSalesManGUIhAL(new BackAction());
	}
	
	/**
	 * fill table with details 
	 * @param cust ->CustomerHistoryModel
	 */
	public void handleDBResult(CustomerHistoryModel cust)
	{
		System.out.println("aaaaaaa");
		String detS = "Name:"+ cust.getFname() + "    "+ cust.getLname()+ "             C_id:   "+cust.getCID();
		Object[][] ob =new Object[cust.DetailHis.size()+1][5];
		ob[0][0]=(String)"Cmp_id";
		ob[0][1]=(String)"Quantity";
		ob[0][2]=(String)"Date";
		ob[0][3]=(String)"Comments";
		ob[0][4]=(String)"Time call";
		for (int i=0 ; i< cust.DetailHis.size();i++)
		{
			ob[i+1][0]=(String)cust.DetailHis.get(i).cmp_id;
			ob[i+1][1]=(int)cust.DetailHis.get(i).Qu;
			ob[i+1][2]=(String)cust.DetailHis.get(i).Date1;
			ob[i+1][3]=(String)cust.DetailHis.get(i).Comments;
			ob[i+1][4]=(int)cust.DetailHis.get(i).time_call;
		}
		CustHistGui.setnewDetail(detS,ob);
		
	}
	
	
	
	
	
	
	
	
	/**
	 * warning messege 
	 */
	public void warmassege()
	{
		CustHistGui.warning_messege();
	}
	/**
	 * search a customer 
	 * @author liran
	 *
	 */
	class SearchAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			if (CustHistGui.GetTextField().equals(""))
			{
				CustHistGui.warning_messege();
				return ;
			}
			else {
				
				  Envelope en = new Envelope(CustHistGui.GetTextField(),"SearchHitory");
 				 sendToServer(en);
 				CcrmApp.clien.setCurrObj(tempcon);
				 }		
		}
	}
	class BackAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			CustHistGui.dispose();
			backCon.GetSalesManGui().setVisible(true);
		}
	}
}
